// pages/article/article.js
//参考https://blog.csdn.net/sophie_u/article/details/71745125
var util = require('../../utils/util.js')
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    tabData: [],
    articleMain: [],
    currentPage: 1,
    clientHeight: 0  

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          winHeight: calc,
          clientHeight: clientHeight
        });
      }
    });
    this.getTabData();
    // this.getAtrticleMain();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    this.loadmore = this.selectComponent("#loadmore");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current,
      currentPage: 1
    });
    this.checkCor();
    const tabData = this.data.tabData;
    this.getAtrticleMain(tabData[e.detail.current].id, true);
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    const tabData = this.data.tabData;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur,
        currentPage: 1
      })
      this.getAtrticleMain(tabData[cur].id, true);
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 2) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  getAtrticleMain: function (catalog, isRefresh) {//获取首页推荐列表
    const that = this;
    var pagenum = this.data.currentPage;
    const url = "/apid/r/135/" + pagenum;
    var articleMain = this.data.articleMain;
    util.DoCommand(url, "catalog=" + catalog, null, function (data) {
      var pagenum = that.data.currentPage;
      if (data.length < app.globalData.pagesize && pagenum != 1) {
        pagenum = pagenum - 1;
      }
      if (isRefresh == true) {
        articleMain= data
      } 
      else {
        //合并数据
        for (var i in data) {
          var _id = data[i].id;
          var hasCount = 0;//标识是否已存在
          for (var j in articleMain) {
            var _nid = articleMain[j].id;
            if (_nid == _id) {
              hasCount++;
            }
          }
          //如果不存在则追加数据
          if (hasCount < 1) {
            articleMain.push(data[i]);
          }
        }
      }
      that.setData({
        articleMain: articleMain,
        currentPage: pagenum
      })
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.loadmore.hide();
    },null,that)
  },//获取tab数据
  getTabData: function () {
    const that = this;
    const url = "/apid/r/134";
    util.DoCommand(url, null, null, function (data) {
      that.setData({
        tabData: data,
      })
      that.getAtrticleMain(data[0].id, true);
    }, null, that)
  },
  tapArticle: function (e) {//点击文章跳转文章详情
    //获取点击id
    const that=this;
    var articleid = e.currentTarget.dataset.articleid;
    const newid = util.NewGuid();
    const url = "/apid/c/144";
    const accountid = app.globalData.accountid;
    const params = "articleid=" + articleid + "&accountid=" + accountid + "&id=" + newid
    //调用接口记录阅读状态
    util.DoCommand(url, params, null, function (data) { }, null, that)
    wx.navigateTo({
      url: '/pages/articledetail/articledetail?id=' + articleid
    })
  },
  onPullDownRefresh: function () {//上下拉刷新加载
    wx.showNavigationBarLoading() //在标题栏中显示加载
    const tabData = this.data.tabData;
    this.setData({
      currentPage: 1
    })
    const currentTab = this.data.currentTab;
    this.getAtrticleMain(tabData[currentTab].id, true);
  },
  onReachBottom: function (e) {//上拉
    this.loadmore.show();
    const pagenum = this.data.currentPage + 1
    const tabData = this.data.tabData;
    this.setData({
      currentPage: pagenum
    })
    const currentTab = this.data.currentTab;
    this.getAtrticleMain(tabData[currentTab].id, false);
  }
})